home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / size.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  72 lines

  1. /* size - tell size of an object file        Author: Andy Tanenbaum */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5.  
  6. #define HLONG            8    /* # longs in the header */
  7. #define TEXT             2
  8. #define DATA             3
  9. #define BSS              4
  10. #define CHMEM            6
  11. #define MAGIC       0x0301    /* magic number for an object file */
  12. #define SEPBIT  0x00200000    /* this bit is set for separate I/D */
  13.  
  14. int heading;            /* set when heading printed */
  15. int error;
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.   int i;
  22.  
  23.   if (argc == 1) {
  24.     size("a.out");
  25.     exit(error);
  26.   }
  27.   for (i = 1; i < argc; i++) size(argv[i]);
  28.   exit(error);
  29. }
  30.  
  31.  
  32.  
  33. size(name)
  34. char *name;
  35. {
  36.   int fd, separate;
  37.   long head[HLONG], dynam, allmem;
  38.  
  39.   if ((fd = open(name, O_RDONLY)) < 0) {
  40.     stderr3("size: can't open ", name, "\n");
  41.     return;
  42.   }
  43.   if (read(fd, head, sizeof(head)) != sizeof(head)) {
  44.     stderr3("size: ", name, ": header too short\n");
  45.     error = 1;
  46.     close(fd);
  47.     return;
  48.   }
  49.   if ((head[0] & 0xFFFFL) != MAGIC) {
  50.     stderr3("size: ", name, " not an object file\n");
  51.     close(fd);
  52.     return;
  53.   }
  54.   separate = (head[0] & SEPBIT ? 1 : 0);
  55.   dynam = head[CHMEM] - head[TEXT] - head[DATA] - head[BSS];
  56.   if (separate) dynam += head[TEXT];
  57.   allmem = (separate ? head[CHMEM] + head[TEXT] : head[CHMEM]);
  58.   if (heading++ == 0) prints("  text\t  data\t   bss\t stack\tmemory\n");
  59.   printf("%6ld\t%6ld\t%6ld\t%6ld\t%6ld\t%s\n",
  60.          head[TEXT], head[DATA], head[BSS], dynam, allmem, name);
  61.   close(fd);
  62. }
  63.  
  64. stderr3(s1, s2, s3)
  65. char *s1, *s2, *s3;
  66. {
  67.   std_err(s1);
  68.   std_err(s2);
  69.   std_err(s3);
  70.   error = 1;
  71. }
  72.